namespace SimpleWebServices.Web
{
    using System;
    using System.Web;
    using SimpleWebServices.Security.Principal;

    /// SimpleWebServices.Web.CookieAuthenticationModule`1[[SimpleWebServices.Security.Principal.TestUser, SimpleWebServices.Mvc.Facts]]
    public sealed class CookieAuthenticationModule<T> : IHttpModule
        where T : User
    {
        public void Dispose()
        {
        }

        public void Init(HttpApplication context)
        {
            if (null == context)
            {
                throw new ArgumentNullException("context");
            }

            context.AuthenticateRequest += new EventHandler(this.OnAuthenticateRequest);
        }

        private void OnAuthenticateRequest(object source, EventArgs args)
        {
            User.SetFromHttpRequestCookie<T>();
        }
    }
}